<?php
/**
 * 权限
 */
namespace app\model;

use think\Model;

class UserModel extends Model
{
   protected $name = 'admin';
  /**
    * 通过账号获取账号信息
    *
    */
   public function GetUserByMobile($mobile,$id,$from=1){
    $user=new Admin;
    //注册
    if($from==1){
    	$where="a.mobile='$mobile' and a.status !=-1 and b.role_id=$id";
    }
    //登录
    if($from==2){
    	$where="a.mobile='$mobile' and a.status =1 and b.status=1 and b.role_id=$id";
    }
    $result=$user->field('a.id,a.mobile,a.password,a.status')->alias('a')->join('admin_role b','a.id=b.admin_id','inner')->where($where)->find();
    return $result;
   }
   /**
    * 管理员详情
    *
    */
   public function detail($id){
      $user = Admin::find($id);
      return $user;
   }
  /**
   * 添加
   *
  */
   public function AddData($param){
      $user = new Admin;
      $param['create_time']=date('Y-m-d H:i:s');

      $field=['nickname','mobile'];
      if(isset($param['password'])&&!empty($param['password'])){
         $field[]='password';
         $param['password']=md5($param['password']);
      }
      
      $user->allowField($field)->save($param);
      return $user->id; 
   }
   /**
   * 编辑
   *
  */
  public function EditData($param,$id){
   $user = Admin::find($id);
   $param['update_time']=date('Y-m-d H:i:s');

   $field=['nickname','mobile'];
   if(isset($param['password'])&&!empty($param['password'])){
      $field[]='password';
      $param['password']=md5($param['password']);
   }
   $user->allowField( $field)->save($param);
   return $id; 
  }
  /**
   * 修改禁用状态
   *
  */
  public function UpdateStatus($id){
   $user = Admin::find($id);
   $user->status=$user->status==1?0:1;
   $user->save();
   return true; 
  }
 /**
   * 删除
   *
  */
  public function Del($param){
   $update=[];
   foreach($param as $val){
       $update[]=['id'=>$val,'status'=>-1,'delete_time'=>date('Y-m-d H:i:s')];
   }
   if(!empty($update)){
      $user = new Admin;
      $user->saveAll($update);
   }
   return true; 
  }
}